package com.hlpedu.mapper;

import com.hlpedu.pojo.Employee;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * 模拟持久层，封装数据
 */
@Repository
public class EmployeeMapper {
    //初始化员工信息
    static Map<Integer, Employee> employees = null;

    static {
        employees = new HashMap<>();
        employees.put(1001, new Employee(1001, "铁扇公主", "女", "aa@163.com"));
        employees.put(1002,
                new Employee(1002, "西伯利亚狼", "男", "xl@126.com"));
        employees.put(1003,
                new Employee(1003, "山炮", "男", "sp@163.com"));
        employees.put(1004,
                new Employee(1004, "唐僧", "男", "ts@126.com"));
        employees.put(1005,
                new Employee(1005, "光头强", "男", "gtq@163.com"));
        employees.put(1006,
                new Employee(1006, "熊二", "男", "xe@163.com"));
        employees.put(1007,
                new Employee(1007, "熊大", "女", "xd@163.com"));
        employees.put(1008,
                new Employee(1008, "奔波儿灞", "女", "bbrba@163.com"));
        employees.put(1009,
                new Employee(1009, "灞波儿奔", "女", "babrb@163.com"));
        employees.put(1010,
                new Employee(1010, "嫦娥妹妹", "女", "cemeimei@163.com"));
    }

    //先获取初始化数据中的最大的id值
    private static Integer initId = 1010;

    //查询所有员工信息
    public Collection<Employee> queryAllEmp() {
        //values()获取集合中所有的键值对
        return employees.values();

    }
    //根据id查询指定用户信息
    public Employee queryEmpById(Integer id){
        return employees.get(id);
    }
    //新增员工的方法
    public void saveEmp(Employee employee) {
//        employees.put(1010,
//                new Employee(1010,"嫦娥妹妹","女","cemeimei@163.com"));
        if (employee.getId() == null) {
            employee.setId(++initId);
        }
        System.out.println(employee.getId());
        employees.put(employee.getId(), employee);
    }

    //根据id更新新用户信息
    public Employee updateEmp(Integer id) {
        return employees.get(id);
    }

    //根据id删除用户信息
    public void deleteEmp(Integer id) {
        employees.remove(id);
    }
}

